package com.作业.Day21;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Scanner;

/**
 * 通过反射定义数组
 */
public class Homework2 {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
        System.out.println("界面");
        System.out.println("请输入数组长度：");
        Scanner scanner = new Scanner(System.in);
        int i1 = scanner.nextInt();
        System.out.println("请输入数组的类型");
        String name = scanner.next();
        Class aClass = Class.forName(name);
        // 使用Array.newInstance创建指定类型和长度的数组
        Object array = Array.newInstance(aClass, i1);

        for (int i = 0; i < i1; i++) {
            System.out.println("请输入第"+(i+1)+"个元素");
            Array.set(array,i,scanner.next());
        }
        for (int i = 0; i < i1; i++) {
            System.out.println(Array.get(array,i));
        }

    }
}
